/**
 * 
 */
package com.jdy.haoduoai.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoai.activity.RadioDetailActivity;
import com.jdy.haoduoai.activity.StoryDetailActivity;
import com.jdy.haoduoai.model.AlbumCollectionItem;
import com.jdy.haoduoai.util.ImageLoader;
import com.jdy.haoduoai.util.Tools;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

//import com.jdy.haoduoai.R;

/**
 * @author zp 2016-5-24 下午2:40:29
 */

public class AnimationGridViewAdapter extends BaseAdapter {

  private final Context mContext;

  private final ArrayList<AlbumCollectionItem> mList;

  private final SparseArray<View> saArray = new SparseArray<View>();

  public AnimationGridViewAdapter(Context mContext,
      ArrayList<AlbumCollectionItem> mList) {
    this.mContext = mContext;
    this.mList = mList;
  }

  @Override
  public int getCount() {
    if (mList == null) {
      return 0;
    } else {
      return this.mList.size();
    }
  }

  @Override
  public Object getItem(int position) {
    if (mList == null) {
      return null;
    } else {
      return this.mList.get(position);
    }
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @SuppressLint({ "NewApi", "InflateParams" })
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    convertView = saArray.get(position);
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = LayoutInflater.from(this.mContext).inflate(
          R.layout.horizontallist_item, null, false);
      holder.storyimg = (ImageView) convertView.findViewById(R.id.img);
      holder.storytittle = (TextView) convertView.findViewById(R.id.tittle);
      convertView.setTag(holder);
      saArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    if (this.mList != null) {
      final AlbumCollectionItem aci = this.mList.get(position);
      holder.storyimg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          // TODO Auto-generated method stub
          Intent intent = new Intent();
          if (aci.getName().equals("宝宝电台")) {
            intent.setClass(mContext, RadioDetailActivity.class);
          } else {
            intent.setClass(mContext, StoryDetailActivity.class);
          }
          intent.putExtra("id", aci.getId());
          mContext.startActivity(intent);
        }
      });
      holder.storytittle.setText(aci.getName());

      final ImageView iiiimg = holder.storyimg;

      if (Tools.isNotEmpty(aci.getCover()))
        Picasso
            .with(mContext)
            .load(aci.getCover())
            .placeholder(
                mContext.getResources().getDrawable(R.drawable.default_cover))
            .error(
                mContext.getResources().getDrawable(R.drawable.default_cover))
            .into(holder.storyimg, new Callback() {

              @Override
              public void onSuccess() {
                // TODO Auto-generated method stub

              }

              @Override
              public void onError() {
                if (mImageLoader == null) {
                  mImageLoader = new ImageLoader(mContext);
                }
                mImageLoader.DisplayImage(aci.getCover(), iiiimg, false);

              }
            });
    }
    return convertView;
  }

  private ImageLoader mImageLoader;

  private class ViewHolder {
    ImageView storyimg;
    TextView storytittle;
  }
}
